package eoeProject.edenOfEast.page.analyses.analysesTop.component;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.IAjaxCallDecorator;
import org.apache.wicket.ajax.calldecorator.AjaxCallDecorator;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ListChoice;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.Model;

import eoeProject.edenOfEast.bean.EOEAnalysesBean;
import eoeProject.edenOfEast.core.DBConnector;
import eoeProject.edenOfEast.core.SQLMaker;
import eoeProject.edenOfEast.dataBase.DB_EOE_ANALYSES_CATEGORY;

//@AuthorizeInstantiation("ADMIN")
public class AnalysesTopInputPanel extends Panel {
	private static final long serialVersionUID = 1L;

	private Form<Void> form = new Form<Void>("form");

	private Model<String> title = new Model<String>("");

	private Model<String> category = new Model<String>("");

	private Model<String> about = new Model<String>("");

	private Model<String> tag1 = new Model<String>("");

	private Model<String> tag2 = new Model<String>("");

	private Model<String> tag3 = new Model<String>("");

	private FeedbackPanel feedback = new FeedbackPanel("feedback");

	private Label message = new Label("message", "");

	private boolean formExistFlag = false;

	public AnalysesTopInputPanel(String id, EOEAnalysesBean bean) {
		this(id);
		title.setObject(bean.getTitle());
		category.setObject(bean.getCategoryName());
		about.setObject(bean.getAbout());
		tag1.setObject(bean.getTag1());
		tag2.setObject(bean.getTag2());
		tag3.setObject(bean.getTag3());
		formExistFlag = true;
	}

	@SuppressWarnings("rawtypes")
	public AnalysesTopInputPanel(String id) {
		super(id);

		feedback.setOutputMarkupId(true);
		message.setOutputMarkupId(true);
		message.setVisible(false);

		TextField<String> titleForm = new TextField<String>("titleForm", title);
		titleForm.setRequired(true);
		form.add(titleForm);

		TextArea<String> aboutForm = new TextArea<String>("aboutForm", about);
		aboutForm.setRequired(true);
		form.add(aboutForm);

		TextField<String> tag1Form = new TextField<String>("tag1Form", tag1);
		form.add(tag1Form);

		TextField<String> tag2Form = new TextField<String>("tag2Form", tag2);
		form.add(tag2Form);

		TextField<String> tag3Form = new TextField<String>("tag3Form", tag3);
		form.add(tag3Form);

		List<String> list = new ArrayList<String>();
		SQLMaker sql = new SQLMaker(DB_EOE_ANALYSES_CATEGORY.TABLE_NAME,
				SQLMaker.SQL_TYPE_SELECT);
		ResultSet rs = DBConnector.getInstance().get(sql.getSQL());
		try {
			while (rs.next()) {
				list.add(rs.getString(DB_EOE_ANALYSES_CATEGORY.NAME));
			}

			rs.close();
		} catch (SQLException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		}

		ListChoice<String> categoryList = new ListChoice<String>(
				"categoryList", category, list);
		categoryList.setRequired(true);
		form.add(categoryList);

		form.add(makeAnalysesButton);
		form.add(feedback);
		form.add(message);
		add(form);
		Label formExist = new Label("formExist");
		formExist.add(new AttributeModifier("style",
				new AbstractReadOnlyModel() {
					/**
			 *
			 */
					private static final long serialVersionUID = 3406851160722465174L;

					@Override
					public Object getObject() {
						if (formExistFlag)
							return "color :#000000;";
						else
							return "color :#FFFFFF;";
					}
				}));
		add(formExist);
	}

	private AjaxButton makeAnalysesButton = new AjaxButton("makeAnalysesButton") {
		/**
		 *
		 */
		private static final long serialVersionUID = -2942520388218435030L;

		@SuppressWarnings("rawtypes")
		@Override
		protected void onSubmit(AjaxRequestTarget target, Form form) {
			setResponsePage(form.getPage());
			EOEAnalysesBean bean = new EOEAnalysesBean();

			bean.setTitle(title.getObject());
			bean.setCategoryName(category.getObject());
			bean.setAbout(about.getObject());
			if (tag1.getObject() != null)
				bean.setTag1(tag1.getObject());
			if (tag2.getObject() != null)
				bean.setTag2(tag2.getObject());
			if (tag3.getObject() != null)
				bean.setTag3(tag3.getObject());

			target.getPage()
					.get("analysesTopPanel")
					.get("analysesTopFormPanel")
					.replaceWith(
							new AnalysesTopComfirmPanel("analysesTopFormPanel",
									bean).setOutputMarkupId(true));

		}

		@SuppressWarnings({ "rawtypes" })
		@Override
		protected void onError(final AjaxRequestTarget target, final Form form) {
			target.add(feedback);
		}

		@Override
		protected IAjaxCallDecorator getAjaxCallDecorator() {
			return new AjaxCallDecorator() {
				/**
				 *
				 */
				private static final long serialVersionUID = 1L;

				@Override
				public CharSequence decorateScript(Component c,
						CharSequence script) {
					return super.decorateScript(c, "savelocate(); " + script);
				}
			};
		}
	};

}
